﻿using System;
using System.Collections.Generic;
using System.Management;
using System.Net.NetworkInformation;
using System.Text;

namespace ST.Common.Util
{
    public class WMIHelper
    {
        /// <summary>
        /// <summary>
        /// 获取主板号
        /// </summary>
        public static string GetMotherboardNumber()
        {
            string strbNumber = string.Empty;
            ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_baseboard");
            foreach (ManagementObject mo in mos.Get())
            {
                strbNumber = mo["SerialNumber"].ToString();

            }
            return strbNumber;
        }

        /// <summary>
        /// 获取CPU序列号代码
        /// </summary>
        /// <returns></returns>
        public static string GetCpuID()
        {
            try
            {
                string cpuInfo = "";//cpu序列号 
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                }
                moc = null;
                mc = null;
                return cpuInfo;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }

        /// <summary>
        /// 获取网卡MAC地址列表 
        /// </summary>
        public static List<string> GetMacAddress()
        {
            List<string> lst = new List<string>();
            try
            {
                //获取网卡硬件地址 
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        lst.Add(mo["MacAddress"].ToString());
                    }
                }
                moc = null;
                mc = null;
                return lst;
            }
            catch
            {
            }
            finally
            {
            }
            return lst;
        }

        public static List<string> GetMacAddressByNetworkInformation()
        {
            List<string> lst = new List<string>();
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in nics)
            {
                if (!adapter.GetPhysicalAddress().ToString().Equals(""))
                {
                    lst.Add(adapter.GetPhysicalAddress().ToString());
                }
            }
            return lst;
        }

        /// <summary>
        /// 获取IP地址
        /// </summary>
        public static string GetIPAddress()
        {
            try
            {
                string st = "";
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        System.Array ar;
                        ar = (System.Array)(mo.Properties["IpAddress"].Value);
                        st = ar.GetValue(0).ToString();
                        break;
                    }
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "";
            }
            finally
            {
            }

        }

        /// <summary>
        /// 获取硬盘ID
        /// </summary>
        public static List<string> GetDiskID()
        {
            List<string> lst = new List<string>();
            //try
            //{
                
            //    ManagementClass mc = new ManagementClass("Win32_DiskDrive");
            //    ManagementObjectCollection moc = mc.GetInstances();
            //    foreach (ManagementObject mo in moc)
            //    {
            //        lst.Add(Convert.ToString(mo.Properties["Model"].Value));
            //    }
            //    moc = null;
            //    mc = null;
            //    return HDid;
            //}
            //catch
            //{ }
            //finally
            //{
            //}
            return lst;
        }

        /// <summary> 
        /// 操作系统的登录用户名 
        /// </summary> 
        public static List<string> GetUserName()
        {
            List<string> lst = new List<string>();
            try
            {
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    lst.Add(Convert.ToString(mo["UserName"]));
                }
            }
            catch { }
            return lst;
        }


        /// <summary> 
        /// PC类型 
        /// </summary> 
        /// <returns></returns> 
        public static string GetSystemType()
        {
            try
            {
                string st = "";
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {

                    st = mo["SystemType"].ToString();

                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }

        /// <summary> 
        /// 物理内存 
        /// </summary> 
        /// <returns></returns> 
        public static string GetTotalPhysicalMemory()
        {
            try
            {
                string st = "";
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    st = mo["TotalPhysicalMemory"].ToString();
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }
        }

        /// <summary>
        /// 获取电脑名
        /// </summary>
        /// <returns></returns>
        public static string GetComputerName()
        {
            try
            {
                return System.Environment.GetEnvironmentVariable("ComputerName");
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }
        }
    }
}
